'use strict';
var fayeClient = require('./faye-client');
var {
  posFayeLogger
} = require("./../services/log");
var {
  Response
} = require("./../services/Response");

module.exports = function(app, router) {
  var client = fayeClient(app);
  router.post('/confirm-order/longitude/:longitude/latitude/:latitude',
    function*(req, res, next) {
      var order = req.body;
      // console.log('/confirm-order/longitude/:longitude/latitude/:latitude',
      // req.params.storeId);
      try {
        let reply = yield client.request('/stores/' + req.params.storeId +
          '/order/placement', {
            order: order,
            position: {
              longitude: req.params.longitude,
              latitude: req.params.latitude
            }
          }, 10000);
        if (reply.success != true) {
          posFayeLogger.error(reply);
        }
        res.json(reply);
      } catch (e) {
        let error;
        error = Response(500, "未知错误", e.stack || e.statusMessage || e);
        posFayeLogger.error(error)
        res.json({
          success: false,
          message: '下单超时,请联系商家！',
          data: ''
        });
      }
    });
}

// 'use strict';
// var fayeClient = require('./faye-client');

// module.exports = function (app, router) {
//   var client = fayeClient(app);
//   router.post('/confirm-order', function*(req, res, next){
//     var order = req.body;
//     let reply = yield client.request('/stores/'+req.params.storeId+'/order/placement', {order:order});
//     console.log('reply:',reply);
//       if (reply.success == false) {
//       	return res.status(500).json({message:reply.message});
//       }
//       res.json(reply);
//    });
// }
